//
//  CXShopViewController.m
//  Meituan_OC
//
//  Created by Tony on 16/9/25.
//  Copyright © 2016年 Tony. All rights reserved.
//

#import "CXShopViewController.h"
#import "CXShopCategoryView.h"
#import "CXShopFoodViewController.h"


#define kHeaderHeight 124

@interface CXShopViewController () <UIGestureRecognizerDelegate, UIScrollViewDelegate>

/// 顶部视图
@property (nonatomic, strong) UIView * vHeader;
/// 中间分类视图
@property (nonatomic, strong) CXShopCategoryView * vCategory;
/// 下方内容视图
@property (nonatomic, strong) UIScrollView * vContent;

@end

@implementation CXShopViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"全聚德（上地店）";
}

- (void)viewDidAppear:(BOOL)animated {
    
    self.navigationController.navigationBar.alpha = 0;
}





#pragma mark - UIScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    //
    if (scrollView.isDragging || scrollView.isTracking || scrollView.isDecelerating) {
        _vCategory.offSetX = scrollView.contentOffset.x / 3;
    }
}

#pragma mark - methods

// 分类标签视图的监听方法
-(void) vCategoryAction:(CXShopCategoryView *)vCategory {
    NSInteger index = vCategory.selectedIndex;
    CGFloat vContentOffsetX = index * self.view.bounds.size.width;
//    self.vContent.contentOffset = CGPointMake(vContentOffsetX, 0);
    [self.vContent setContentOffset:CGPointMake(vContentOffsetX, 0) animated:YES];
}


#pragma mark - 拖拽手势

- (void) panGesture:(UIPanGestureRecognizer *)panGesture {
    
    CGPoint translation = [panGesture translationInView:self.view];
    
    // 手势识别复位！！（需要复位的三个手势：旋转、缩放、平移）否则会叠加！！
    [panGesture setTranslation:CGPointZero inView:self.view];
    
    CGFloat height = self.vHeader.bounds.size.height + translation.y;
    
    if (height > kHeaderHeight || height < 64) {
        return;
    }
    
    CGFloat minHeight = 64;
    CGFloat scale = 1 - (height - minHeight) / (kHeaderHeight - minHeight);
    
    // 修正导航栏的浅色透明度
    scale = (scale < 0.05) ? 0 : scale;
    
    // 根据手势状态修改顶部视图的高度和导航栏透明度
    switch (panGesture.state) {
        case UIGestureRecognizerStateBegan:
        case UIGestureRecognizerStateChanged:
            [self.vHeader mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(height);
            }];
            self.navigationController.navigationBar.alpha = scale;
            break;
            
        default:
            break;
    }
}

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}


#pragma mark - setupUI
-(void) setupUI {
    //
    /*
     *  逻辑分析
      1. 设置背景颜色
      2. 设置顶部视图
      3. 设置分类标签视图
      4. 设置内容视图
     */
    
    // 1. 设置背景颜色
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 2. 设置顶部视图
    _vHeader = [[UIView alloc] init];
    _vHeader.backgroundColor = [UIColor orangeColor];
    //
    [self.view addSubview:_vHeader];
    
    [_vHeader mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.trailing.equalTo(self.view);
        make.height.mas_equalTo(kHeaderHeight);
    }];
    
    // 3. 设置标签分类视图
    CXShopCategoryView * vCategory = [[CXShopCategoryView alloc] init];
    self.vCategory = vCategory;
    // 添加监听事件
    [self.vCategory addTarget:self action:@selector(vCategoryAction:) forControlEvents:UIControlEventValueChanged];
    //
    [self.view addSubview:vCategory];
    
    [vCategory mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.view);
        make.top.equalTo(_vHeader.mas_bottom);
        make.height.mas_equalTo(37);
    }];
    
    // 4. 设置内容视图
    UIScrollView *vContent = [self setupContentView];
    self.vContent = vContent;
    vContent.delegate = self;
    
    //
    [self.view addSubview:vContent];
    
    [vContent mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(vCategory.mas_bottom);
        make.bottom.leading.trailing.equalTo(self.view);
    }];
    
    // 5. 添加拖拽手势
    UIPanGestureRecognizer * panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];
    
    // 解决手势共存(拖拽手势与scrollView的拖动手势)
    panGesture.delegate = self;

    [self.view addGestureRecognizer:panGesture];
}


#pragma mark 设置内容视图
-(UIScrollView *) setupContentView {
    /*
     *  给scrollView纯代码布局，有两种方法
     1. 直接设置scrollView的contentSize；
     2. 设置scrollView直接子视图的布局来简介设置contentSize。
     */
    //
    UIScrollView * sv = [[UIScrollView alloc] init];
    sv.pagingEnabled = YES;
    sv.bounces = NO;
//    sv.backgroundColor = [UIColor yellowColor]; // magentaColor 品红
    
    // 添加一个sizeView，作为sv的直接唯一子控件，sv中所有的其他子控件都添加到这个vSize视图中，便于布局。
    UIView * vSize = [[UIView alloc] init];
    [sv addSubview:vSize];
    //
    [vSize mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(sv);
        make.width.mas_equalTo(sv).multipliedBy(3);
        make.height.mas_equalTo(sv);
    }];
    
    // 视图中循环添加控制器（多控制器）
    NSArray * childrenArr = @[@"CXShopFoodViewController", @"CXCommentViewController", @"CXShopInfoViewController"];
    NSMutableArray<UIViewController *> *vcArr = [NSMutableArray array];
    
    for (NSString * vcName in childrenArr) {
        
        Class cls = NSClassFromString(vcName);
        UIViewController * vc = [[cls alloc] init];
        
        NSAssert([vc isKindOfClass:[UIViewController class]], @"%@ 不能转成UIViewController类 ", vcName);
        [vcArr addObject:vc];
        
        [self cx_addChildViewController:vc intoView:vSize];
    }

    // 子控制器的view布局
    [vcArr[0].view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.bottom.mas_equalTo(vSize);
        make.width.mas_equalTo(sv);
    }];
    
    [vcArr[1].view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.top.equalTo(vcArr[0].view);
        make.leading.equalTo(vcArr[0].view.mas_trailing);
    }];
    [vcArr[2].view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.top.equalTo(vcArr[1].view);
        make.leading.equalTo(vcArr[1].view.mas_trailing);
    }];
    
    
    return sv;
}


//-(void) cx_addChildViewController:(UIViewController *)childVc intoView:(UIView *)toView {
//    // 1. 添加子控制器
//    [self addChildViewController:childVc];
//    // 2. 添加子控制器的视图
//    [toView addSubview:childVc.view];
//    // 3. 完成添加
//    [childVc didMoveToParentViewController:self];
//}









@end
